<?php
namespace app\beautiful\logic;

use app\beautiful\model\logs\MemberBalanceLogsModel;
use app\beautiful\model\logs\MemberCouponLogsModel;
use app\beautiful\model\logs\MemberIntegralLogsModel;
use app\beautiful\model\logs\MemberSupplementModel;
use app\beautiful\model\logs\StoreWriteOffModel;
use app\beautiful\model\member\MemberBalanceModel;
use app\beautiful\model\member\MemberIntegralModel;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\setting\SettingSmsModel;
use app\beautiful\model\StaffModel;
use app\beautiful\model\logs\SmsRechargeLogsModel;
use app\common\logic\SmsLogic;
use app\common\model\account\AccountModel;
use app\common\model\ShopModel;
use app\beautiful\model\logs\ProjectWriteOffModel;
use app\beautiful\model\member\MemberOnceCardItemModel;
use app\beautiful\model\member\MemberOnceCardModel;
use app\beautiful\model\member\MemberSnapUpModel;

class LogsLogic{

    private static $balanceType=[
        1=>'在线充值',
        2=>'员工充值',
        3=>'后台充值',
        4=>'消费赠送',
        5=>'买单扣除',
        6=>'员工核销',
        7=>'后台核销',
        8=>'商城购物',
        9=>'拼团退款',
        10=>'后台补单充值',
        11=>'后台补单消耗',
        12=>'员工补单充值',
        13=>'员工补单消耗',
        14=>'抢购支付',
        15=>'次卡支付',
        20=>'收银充值',
        21=>'收银核销',

    ];
    public static function balance($shop_id,$member_id,$type,$type_id,$original_deduction,$balance,$give_balance,$remarks,$operate_id)
    {
        $member_balance_logs=new MemberBalanceLogsModel();
        $member_balance_logs->shop_id=$shop_id;
        $member_balance_logs->member_id=$member_id;
        $member_balance_logs->type=$type;
        $member_balance_logs->type_id=$type_id;
        $member_balance_logs->info=self::$balanceType[$type_id] ?? '其他方式';
        if($type==-1) $member_balance_logs->original_deduction=$original_deduction;
        $member_balance_logs->balance=$balance;
        if($type==1) $member_balance_logs->give_balance=$give_balance;
        $member_balance_logs->remarks=$remarks;
        $member_balance_logs->operate_id=$operate_id;
        if(in_array($type_id,[2,6,12,13,20,21])){
            if(null === $operator = StaffModel::find($operate_id)){
                return false;
            }
            $member_balance_logs->operate_type=1;
            $member_balance_logs->operate_mobile=$operator->mobile;
            $member_balance_logs->operate_name=$operator->name;
        }

        if(in_array($type_id,[3,7,10,11])){
            if(null === $operator = AccountModel::find($operate_id)){
                return false;
            }
            $member_balance_logs->operate_type=2;
            $member_balance_logs->operate_mobile=$operator->mobile;
            $member_balance_logs->operate_name=$operator->username;
        }
        $member_balance_logs->save();

        //短信处理
        try{
            $sms_setting = SettingSmsModel::where([['shop_id','=',$shop_id]])->find();
            if(null === $sms_setting) return true;
            $sms_logic= new SmsLogic();
            $member = MemberModel::find($member_id);
            if($type==1){
                if($sms_setting->vip_balance_change_notice==1){
                    $params=[
                        'mobile'=>$member->mobile,
                        'balance'=>$balance,
                        'name'=> $member_balance_logs->operate_name ?? $member->nick_name,
                        'source'=>$member_balance_logs->info
                    ];
                    $sms_logic->send($shop_id,$sms_setting->boss_mobiles,$params,'mbalance');
                }
            }else{
                if($sms_setting->balance_change_notice==1){
                    $shop = ShopModel::find($shop_id);
                    $remain_balance = MemberBalanceModel::where([
                            ['shop_id','=',$shop_id],
                            ['member_id','=',$member->member_id],
                            ['valid_bg_time','<=',time()]
                        ])->sum('remain_balance') ?? 0;
                    $params=[
                        'name'=>$shop->name,
                        'balance1'=>$balance,
                        'balance2'=> $remain_balance,
                    ];
                    $sms_logic->send($shop_id,[$member->mobile],$params,'balance');
                }
            }

        }catch (\Exception $e){
            return true;
        }

        return true;

    }


    private static $integralType=[
        1=>'员工充值',
        2=>'后台充值',
        3=>'充值赠送',
        4=>'员工核销',
        5=>'后台核销',
        6=>'买单抵扣',
        7=>'兑换优惠券',
        8=>'支付赠送',
        9=>'商城支付',
        10=>'取消拼团返回',
        11=>'抢购支付',
        12=>'次卡支付',
        20=>'收银充值',
        21=>'收银核销',
    ]; 
    public static function integral($shop_id,$member_id,$type,$type_id,$integral,$remarks,$operate_id)
    {
        $member_integral_logs=new MemberIntegralLogsModel();
        $member_integral_logs->shop_id=$shop_id;
        $member_integral_logs->member_id=$member_id;
        $member_integral_logs->type=$type;
        $member_integral_logs->type_id=$type_id;
        $member_integral_logs->info=self::$integralType[$type_id] ?? '其他方式';
        $member_integral_logs->integral=$integral;
        $member_integral_logs->remarks=$remarks;
        $member_integral_logs->operate_id=$operate_id;
        if(in_array($type_id,[1,4,20,21])){
            if(null === $operator = StaffModel::find($operate_id)){
                return false;
            }
            $member_integral_logs->operate_type=1;
            $member_integral_logs->operate_mobile=$operator->mobile;
            $member_integral_logs->operate_name=$operator->name;
        }

        if(in_array($type_id,[2,5])){
            if(null === $operator = AccountModel::find($operate_id)){
                return false;
            }
            $member_integral_logs->operate_type=2;
            $member_integral_logs->operate_mobile=$operator->mobile;
            $member_integral_logs->operate_name=$operator->username;
        }
        $member_integral_logs->save();

        //短信处理
        try{
            $sms_setting = SettingSmsModel::where([['shop_id','=',$shop_id]])->find();
            if(null === $sms_setting) return true;
            $sms_logic= new SmsLogic();
            $member = MemberModel::find($member_id);
            if($type==-1){
                if($sms_setting->balance_change_notice==1){
                    $shop = ShopModel::find($shop_id);
                    $remain_integral = MemberIntegralModel::where([
                            ['shop_id','=',$shop_id],
                            ['member_id','=',$member->member_id],
                            ['expire_time','>',time()]
                        ])->sum('remain_integral') ?? 0;
                    $params=[
                        'name'=>$shop->name,
                        'num'=>$integral,
                        'num2'=> $remain_integral,
                    ];
                    $sms_logic->send($shop_id,[$member->mobile],$params,'integral');
                }
            }
        }catch(\Exception $e){
            return true;
        }
        return true;

    }

    private static $couponType=[
        1=>'新人领取',
        2=>'员工赠送',
        3=>'运营者赠送',
        4=>'积分兑换获取',
        5=>'买单支付', //买单支付使用
        6=>'员工核销',
        7=>'后台核销',
        8=>'在线领取',
        9=>'邀请好友赠送',
        10=>'被邀请赠送',
        11=>'充值赠送',
        12=>'商城支付',//商城支付使用
        13=>'取消拼团返回',
        14=>'抢购支付',
        15=>'次卡支付',
        20=>'收银赠送',
        21=>'收银核销'
    ];
    public static function coupon($shop_id,$member_id,$type,$type_id,$member_coupon_id,$remarks,$operate_id)
    {
        $member_coupon_logs=new MemberCouponLogsModel();
        $member_coupon_logs->shop_id=$shop_id;
        $member_coupon_logs->member_id=$member_id;
        $member_coupon_logs->type=$type;
        $member_coupon_logs->type_id=$type_id;
        $member_coupon_logs->info=self::$couponType[$type_id] ?? '其他方式';
        $member_coupon_logs->member_coupon_id=$member_coupon_id;
        $member_coupon_logs->operate_id=$operate_id;
        if(in_array($type_id,[2,6,20,21])){
            if(null === $operator = StaffModel::find($operate_id)){
                return false;
            }
            $member_coupon_logs->operate_type=1;
            $member_coupon_logs->operate_mobile=$operator->mobile;
            $member_coupon_logs->operate_name=$operator->name;
        }

        if(in_array($type_id,[3,7])){
            if(null === $operator = AccountModel::find($operate_id)){
                return false;
            }
            $member_coupon_logs->operate_type=2;
            $member_coupon_logs->operate_mobile=$operator->mobile;
            $member_coupon_logs->operate_name=$operator->username;
        }
        $member_coupon_logs->save();
        return true;

    }
    
    private static $smsRechargeType = [
        1 => '在线购买',
        2 => '系统赠送',
    ];
    
    public static function smsRecharge($shopId,$type,$num,$money){
        $SmsRechargeLogsModel = new SmsRechargeLogsModel();
        $SmsRechargeLogsModel->shop_id = $shopId;
        $SmsRechargeLogsModel->type = $type;
        $SmsRechargeLogsModel->info = self::$smsRechargeType[$type];
        $SmsRechargeLogsModel->sms_num = $num;
        $SmsRechargeLogsModel->money = $money;
        $SmsRechargeLogsModel->save();
        return true;
    }

    private static  $storeOrderWriteOffType=[
        1=>'员工核销',
        2=>'自己核销',
        3=>'管理员核销'
    ];

    public static function storeOrder($shop_id,$member_id,$type_id,$store_order_id,$num,$remain_num,$remarks,$operate_id)
    {
        $store_write_off_model = new StoreWriteOffModel();
        $store_write_off_model->shop_id=$shop_id;
        $store_write_off_model->member_id=$member_id;
        $store_write_off_model->store_order_id=$store_order_id;
        $store_write_off_model->num=$num;
        $store_write_off_model->info=self::$storeOrderWriteOffType[$type_id] ?? '其他方式';
        $store_write_off_model->remain_num=$remain_num;
        $store_write_off_model->operate_id=$operate_id;
        $store_write_off_model->remark=$remarks;
        if(in_array($type_id,[1])){
            if(null === $operator = StaffModel::find($operate_id)){
                return false;
            }
            $store_write_off_model->operate_type=1;
            $store_write_off_model->operate_mobile=$operator->mobile;
            $store_write_off_model->operate_name=$operator->name;
        }

        if(in_array($type_id,[3])){
            if(null === $operator = AccountModel::find($operate_id)){
                return false;
            }
            $store_write_off_model->operate_type=2;
            $store_write_off_model->operate_mobile=$operator->mobile;
            $store_write_off_model->operate_name=$operator->username;
        }
        $store_write_off_model->save();
        return $store_write_off_model->store_write_off_id;

    }

    public static function supplement($shop_id,$member_id,$type_id,$money,$operate_id)
    {
        $supplement_model = new MemberSupplementModel();
        $supplement_model->shop_id = $shop_id;
        $supplement_model->member_id = $member_id;
        $supplement_model->money = $money;
        $supplement_model->member_id=$member_id;
        $supplement_model->operate_id=$operate_id;
        if($type_id==1){
            if(null === $operator = StaffModel::find($operate_id)){
                return false;
            }
            $supplement_model->operate_type=1;
            $supplement_model->operate_mobile=$operator->mobile;
            $supplement_model->operate_name=$operator->name;
        }

        if($type_id==2){
            if(null === $operator = AccountModel::find($operate_id)){
                return false;
            }
            $supplement_model->operate_type=2;
            $supplement_model->operate_mobile=$operator->mobile;
            $supplement_model->operate_name=$operator->username;
        }
        $supplement_model->save();
        return true;
    }

    public static function project($shop_id,$member_id,$source,$service_id,$num,$remain_num,$remark,$operate_type,$operate_id)
    {
        $project_write_off_model=new ProjectWriteOffModel();
        $project_write_off_model->shop_id = $shop_id;
        $project_write_off_model->member_id = $member_id;
        $project_write_off_model->source = $source;
        if($source=='snap_up'){
            if(null === $member_snap_up = MemberSnapUpModel::where([
                    ['shop_id','=',$shop_id],
                    ['member_id','=',$member_id],
                    ['member_snap_up_id','=',$service_id]
                ])->find()){
                return false;
            }
            $project_write_off_model->project_id = $member_snap_up->project_id;
            $project_write_off_model->abbreviation = $member_snap_up->abbreviation;
        }
        if($source=='once_card'){
            if(null === $member_once_card_item = MemberOnceCardItemModel::where([
                    ['shop_id','=',$shop_id],
                    ['member_id','=',$member_id],
                    ['member_once_card_item_id','=',$service_id]
                ])->find()){
                return false;
            }
            if(null === $member_once_card = MemberOnceCardModel::where([
                    ['shop_id','=',$shop_id],
                    ['member_id','=',$member_id],
                    ['member_once_card_id','=',$member_once_card_item->member_once_card_id]
                ])->find()){
                return false;
            }

            $project_write_off_model->project_id = $member_once_card_item->project_id;
            $project_write_off_model->abbreviation = $member_once_card_item->abbreviation;
            $project_write_off_model->member_once_card_id = $member_once_card_item->member_once_card_id;
        }
        $project_write_off_model->service_id = $service_id;
        $project_write_off_model->num = $num;
        $project_write_off_model->remain_num = $remain_num;
        $project_write_off_model->remark = $remark;

        $project_write_off_model->operate_type=$operate_type;
        $project_write_off_model->operate_id=$operate_id;
        if($operate_type==1){
            if(null === $operator = StaffModel::find($operate_id)){
                return false;
            }
            $project_write_off_model->operate_mobile=$operator->mobile;
            $project_write_off_model->operate_name=$operator->name;
        }

        if($operate_type==2){
            if(null === $operator = AccountModel::find($operate_id)){
                return false;
            }

            $project_write_off_model->operate_mobile=$operator->mobile;
            $project_write_off_model->operate_name=$operator->username;
        }
        $project_write_off_model->save();
        return $project_write_off_model->write_off_id;
    }
    
    
}
